package com.example.administrator.pomelostreet.pinpai.fragment;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.administrator.pomelostreet.R;
import com.example.administrator.pomelostreet.pinpai.activity.PinPaiTwoActivity;
import com.example.administrator.pomelostreet.pinpai.adapter.PinPaiRecyclerViewAdapter;
import com.example.administrator.pomelostreet.pinpai.bean.PinPaiBean;
import com.example.administrator.pomelostreet.pinpai.util.Util;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;


public class PinPai_Fragment extends Fragment {

    private Context mContext;
    private RecyclerView mRecyclerView;
    private List<PinPaiBean> mList;
    private PinPaiBean pinPaiBean;
    private PinPaiRecyclerViewAdapter adapter;
    private ArrayList<Integer> Murl=new ArrayList<>();

    public PinPai_Fragment(Context mContext) {
        this.mContext=mContext;
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                  String josn= (String) msg.obj;
                    JsonTo(josn);
                    adapter = new PinPaiRecyclerViewAdapter(mContext,mList);
                    mRecyclerView.setAdapter(adapter);

                    adapter.setOnItemClickListener(new PinPaiRecyclerViewAdapter.OnItemClickListener() {
                        @Override
                        public void onItemClick(View view,int position) {
                            Intent intent=new Intent(getActivity(), PinPaiTwoActivity.class);
                            intent.putExtra("url", Murl.get(position));
                            startActivity(intent);

                        }
                    });

                    break;
            }
        }
    };

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        return inflater.inflate(R.layout.fragment_pin_pai_, container, false);

    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

         mRecyclerView = (RecyclerView) view.findViewById(R.id.pinpai_recylerView);

        //recyclerView的布局管理器
        inivt(mRecyclerView);
        loadNetData();
    }
    //网络请求
    private void loadNetData(){
        new Thread(new Runnable() {
            @Override
            public void run() {
              OkHttpClient okHttpClient = new OkHttpClient();
                Request request=new Request.Builder().url(Util.PinPai_URL).get().build();
                try {
                    Response response=okHttpClient.newCall(request).execute();

                    if(response.isSuccessful()){
                        String json=response.body().string();
                        Message msg=Message.obtain();
                        msg.what=0;
                        msg.obj=json;
                        handler.sendMessage(msg);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    private void JsonTo(String json) {
        try {
            //Gson解析
            JSONArray jsonArray = new JSONObject(json).getJSONObject("data").getJSONArray("item_list");
            mList=new ArrayList<>();
            Gson gson=new Gson();
            for (int i = 0; i < jsonArray.length(); i++) {
                TypeToken<PinPaiBean> token=new TypeToken<PinPaiBean>(){};
                JSONObject obj=jsonArray.getJSONObject(i);
                 pinPaiBean =  gson.fromJson(obj.toString(), token.getType());
                mList.add(pinPaiBean);
                Murl.add(pinPaiBean.getBrand_area_id());
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
      //recyclerView的布局管理器
    private void inivt(RecyclerView mRecyclerView) {
        LinearLayoutManager manager=new LinearLayoutManager(mContext);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(manager);
    }
}
